<?php

#################################################################
#								HTML
#################################################################

/**
 * Register form registed as 'form/register'
 * $vars['name']
 * $vars['username']
 * $vars['email']	
 * $vars['pass1']
 * $vars['pass2']			
 * $vars['display_register_form']	//Boolean
 */

if($_CONTEXT=='HTML'){
	
	if($user_logged!==FALSE){
		$_CSS_STACK->push(blg_get_site_url().'form/register.css', TRUE);
		$_JS_STACK->push(blg_get_site_url().'form/register.js', TRUE);		
		
		$user_logged= blg_get_logged_in_user_guid();	
		
		if(!is_bool($vars['display_register_form']))
			$vars['display_register_form']= FALSE;
			
		if(empty($vars['id']))
			$vars['id']= '';
	
		$width= 150;
			
		//BUILD INPUTS: name, username, email, pass1, pass2
		$input_name= blg_view('element/input', array(
			'name' 	=> 'name',
			'id'	=> 'form_register_name_id',
			'value'	=> $vars['name'],
			'description' 	=> blg_echo('form:register:name'),
			'width' => $width
		));	

		$input_username= blg_view('element/input', array(
			'name' 	=> 'username',
			'id'	=> 'form_register_username_id',
			'value'	=> $vars['username'],
			'description' 	=> blg_echo('form:register:username'),
			'width' => $width
		));			
			
		$input_email= blg_view('element/input', array(
			'name' 	=> 'email',
			'id'	=> 'form_register_email_id',
			'value'	=> $vars['email'],
			'description' 	=> blg_echo('form:register:email'),
			'width' => $width
		));		
		
		$input_pass1= blg_view('element/input', array(
			'name' 	=> 'pass1',
			'id'	=> 'form_register_pass1_id',
			'value'	=> $vars['pass1'],
			'password'	=> TRUE,
			'description' 	=> blg_echo('form:register:pass1'),			
			'width' => $width
		));
		
		$input_pass2= blg_view('element/input', array(
			'name' 	=> 'pass2',
			'id'	=> 'form_register_pass2_id',
			'value'	=> $vars['pass2'],
			'password'	=> TRUE,
			'description' 	=> blg_echo('form:register:pass2'),
			'width' => $width
		));		
		
		//BUTTONS: accept & cancel
		$button_submit= blg_view('element/button', array(
			'text' => blg_echo('accept'),
			'submit' => TRUE,
			'precall' => 'v_form_register(true)'	
		));
	
		$button_submit_cancel= blg_view('element/button', array(
			'text' => blg_echo('cancel'),	
			'action' => "blg_form_register_hide()",	
			'color' => 'grey'
		));
		
		//SECURITY TOKENS
		$security= blg_security_input();
		
		//BUILD FORM
		$content= '
			<div class="blg_header_register_form_div"><img src="'.blg_get_site_url().'_graphics/default/register.png"></div>
			<div class="blg_header_register_form_div">'.$input_name.'</div>
			<div class="blg_header_register_form_div">'.$input_username.'</div>
			<div class="blg_header_register_form_div">'.$input_email.'</div>
			<div class="blg_header_register_form_div">'.$input_pass1.'</div>
			<div class="blg_header_register_form_div">'.$input_pass2.'</div>
			<div class="blg_header_register_form_div_notification">'.blg_echo('register:notification').'</div>
			<div class="blg_header_register_form_div">'.$button_submit.' '.$button_submit_cancel.'</div>
			'.$security.'	
		';
	
		$form= blg_view('element/form', array(
			'action' 			=> blg_get_site_url().'action/user/register',
			'id'				=> 'blg_register_form_id',
			'name'				=> 'blg_register_form',
			'submit_callback'	=> 'return v_form_register(false);',
			'content'			=> $content		 
		));
		
		$register_div= '
		<div id="blg_form_register_id" class="blg_form_register">
			'.$form.'
		</div>
		';	
		
		echo $register_div;
		
		if($vars['display_register_form']===TRUE){
			$code="blg_form_register_show();";
			
			$_JS_CODE->append($code, ON_DOC_READY);
		}
	}
}
#################################################################
#								JS
#################################################################
elseif($_CONTEXT=='JS'){ ?>

function blg_form_register_show(){
	blg_header_login_hide();
	$('#blg_form_register_id').fadeIn(400);
}

function blg_form_register_hide(){
	$('#blg_form_register_id').fadeOut(150);
}

function v_form_register(print_errors){
	error= false;
	
	var name= $('#form_register_name_id').val();
	var username= $('#form_register_username_id').val();
	var email= $('#form_register_email_id').val();
	var pass1= $('#form_register_pass1_id').val();
	var pass2= $('#form_register_pass2_id').val();
	
	if(name.length < 1){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:name:empty')?>');
		error= true;
	}
	
	if(username.length < 1){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:username:empty')?>');
		error= true;
	}
	else if(!v_is_alphanumeric(username)){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:username:not_alphanumeric')?>');
		error= true;		
	}
	if(username.lenght > 0 && username.lenght < <?php echo blg_get_config('username_min_length')?>){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:username:min_length')?>');
		error= true;
	}
	
	if(email.length <1 ){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:email:empty')?>');
		error= true;	
	}
	else if(!v_is_email(email)){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:email:invalid')?>');
		error= true;
	}
		
	if(pass1.length < 1){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:password:empty')?>');
		error= true;
	}
	else if( pass1.length < <?php echo blg_get_config('password_min_length')?>){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:password:min_length')?>');
		error= true;
	}
	
	if(pass1!=pass2){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:password:not_match')?>');
		error= true;
	}
	else if(pass1==username && pass1.length > 0){
		if(print_errors)
			blg_error('<?php echo blg_echo('error:password:equals_username')?>');
		error= true;				
	}
		
	return (!error);
}

<?php }
#################################################################
#								CSS
#################################################################
elseif($_CONTEXT=='CSS'){ ?>

div.blg_form_register{
	position: absolute;
	top: 82px;
	right: 5px;
	
	display: none;
	
	background-color: <?php echo blg_color('grey','B')?>;
	padding: 5px 10px 2px 10px;
	
	border-radius: 12px;
}

div.blg_form_register div.blg_header_register_form_div{
	margin: 5px 0;
	text-align: center;	
}
div.blg_form_register div.blg_header_register_form_div_notification{
	width: 155px;
	color: #444444;
	font-size: 11px;
	line-height: 130%;
	white-space: normal;
	margin: 10px 0;
	text-align: center;	
}

<?php }
		